//app.js
App({
  onLaunch: function () {
    var that = this
    wx.getSetting({
      success: res => {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          that.globalData.isAuthorized =  true
          wx.getUserInfo({
            success: res => {
              console.log(res)
              that.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          that.globalData.isAuthorized =  false
        }
      }
    })
    // 登录
    wx.login({
      success:function(login){
          // 发送请求
          wx.request({
          url: that.globalData.apiUrl + '/Api/GetUser/get_openid', //接口地址
          data: {
            code: login.code,
          },
          method: 'get',
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res);
            //判断用户是否存在
            if(res.data.code == '1'){
              //后台有数据
              that.globalData.userId = res.data.data.id;
              that.globalData.contentId = res.data.data.content_id;
              that.globalData.status = res.data.data.status;
              if(!that.globalData.isAuthorized){
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ 'true',
                })
              }
              if(res.data.data.status != '1'){
                wx.redirectTo({
                  url: '/pages/login/login?status=' + res.data.data.status
                })
                return;
              }
              if(res.data.data.is_manager){
                that.globalData.isManager = res.data.data.is_manager
              }
              if (that.userIdCallback) {
                that.userIdCallback(that.globalData.userId)
              }
              if (that.contentIdCallback) {
                that.contentIdCallback(that.globalData.contentId)
              }
              if (that.statusCallback) {
                that.statusCallback(that.globalData.status)
              }
              if(that.globalData.userInfo){
                  if(res.data.data.nickname != that.globalData.userInfo.nickName || res.data.data.headimgurl != that.globalData.userInfo.avatarUrl){
                      console.log('have change');
                      wx.request({
                          url:that.globalData.apiUrl + '/Api/GetUser/save_user',
                          data:{
                              id:res.data.data.id,
                              headimgurl:that.globalData.userInfo.avatarUrl,
                              nickname:that.globalData.userInfo.nickName
                          },
                          method:'get',
                          header:{
                              'content-type':'application/json'
                          },
                          success(res){
                              console.log(res);
                          }
                      })
                  }
              }

            }else if(res.data.code == '0'){
              that.globalData.openid = res.data.openid
              if(that.globalData.isAuthorized){
                //用户没有登录过 但是已经授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ 'false',
                })
              }
            }else{
              //报错  参数有错
            }
          }
        })

        wx.request({
          url: that.globalData.apiUrl + '/Api/Food/ce', //接口地址
          data: {
          },
          method: 'get',
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res)
            if(res.data.length != 0){
              that.globalData.mainTime = res.data
              if (that.mainTimeCallback) {
                that.mainTimeCallback(that.globalData.mainTime)
              }
            }
          }
        })
      }
    })
  },

  globalData: {
    userInfo: null,
    apiUrl : 'https://food.tfnhg.com',
    isAuthorized: null,
    userId: null,
    contentId: null,
    isManager: null,
    mainTime: [],
    status: null
  }
})
